﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using qlqa.Delegate.FoodDelegate;
using qlqa.Delegate.HoaDonDelegate;
using qlqa.Models.LinQ;
using qlqa.Reports.Dataset;

namespace qlqa.Controllers
{
    public class ReportController : Controller
    {
        HoaDonDelegate _hoaDonDelegate = new HoaDonDelegate();
        FoodDelegate _foodDelegate = new FoodDelegate();
        //
        // GET: /Report/

        public void InReport(int idHoaDon)
        {

            var chiTietHoaDon = _hoaDonDelegate.GetChiTietHoaDonTheoHoaDon(idHoaDon);
            if (chiTietHoaDon.Any())
            {
                var ds = new DbHoaDon();
               
                var dtHoaDon = ds.HoaDon;
                //dtHoaDon.Columns.Add("SoLuong");
                //dtHoaDon.Columns.Add("GiaThuc");
                //dtHoaDon.Columns.Add("MonAn");
                long tongTien = 0;
                int stt = 1;
                foreach (var chiTiet in chiTietHoaDon)
                {
                    var monAn = _foodDelegate.GetFoodById(Convert.ToInt32(chiTiet.IdMon));
                    if (monAn!=null)
                    {
                        var thanhTien = Convert.ToInt64(chiTiet.SoLuong * chiTiet.GiaThuc);
                        DataRow drMonAn = dtHoaDon.NewRow();
                        drMonAn["STT"] = stt++;
                        drMonAn["SoLuong"] = chiTiet.SoLuong;
                        drMonAn["GiaThuc"] = chiTiet.GiaThuc;
                        drMonAn["ThanhTien"] = thanhTien;
                        drMonAn["MonAn"] = monAn.Ten;
                        tongTien += thanhTien;
                        dtHoaDon.Rows.Add(drMonAn);
                    }
                }

                var dtTongTien = ds.TongTien;
                DataRow dr = dtTongTien.NewRow();
                dr["TongTien"] = tongTien;
                dtTongTien.Rows.Add(dr);
                //ds.Tables.Add(dtHoaDon);
                var rpt = new ReportClass { FileName = Server.MapPath("~/Reports/HoaDon.rpt") };
                rpt.Load();
                rpt.SetDataSource(ds);
                rpt.PrintToPrinter(1,true,0,0);
                //string file = Server.MapPath("~") + @"\Reports\HoaDon" + idHoaDon + ".doc";
                //rpt.ExportToDisk(ExportFormatType.WordForWindows, file);
                //Response.ClearContent();
                //Response.ClearHeaders();
                //Response.ContentType = "application/word";
                //Response.AddHeader("Content-Disposition", " filename=HoaDon" + idHoaDon + ".doc");
                //Response.WriteFile(file);
                //Response.Flush();
                //Response.Close();
                //System.IO.File.Delete(file);
            }
        }

    }
}
